package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.utils.SortedListOf;

public class ContactHelper extends HelperBase{
	

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}

private SortedListOf<ContactData> cachedContacts;
	
	public SortedListOf<ContactData> getContacts() {
		if (cachedContacts == null) {
			rebuildCache();	
		}
		return cachedContacts;	
	}
	
	private void rebuildCache() {
	    SortedListOf<ContactData> cachedContacts = new SortedListOf<ContactData>();
		manager.navigateTo().mainPage();
		List<WebElement> rows = driver.findElements(By.xpath("//tr[@name='entry']"));
		for (WebElement row : rows) {
				
		List<WebElement>cells = row.findElements(By.tagName("td"));
		String lastname = cells.get(1).getText();
		String firstname = cells.get(2).getText();
		String email1 = cells.get(3).getText();
		String homephone = cells.get(4).getText();
		cachedContacts.add(new ContactData().withFirstname(firstname));	
		}	
	}
	
	public ContactHelper createContact(ContactData contact) {
		manager.navigateTo().mainPage();
		initContactCreation();
	    fillContactForm(contact);
	    submitContactCreation();
	    returnToHomePage();
	    rebuildCache();
	    return this;
	}
	
	public ContactHelper modifyContact(int index, ContactData contact) {
		manager.navigateTo().mainPage();
		initContactModification(index);
	    fillContactForm(contact);
	    submitContactModification();
	    returnToHomePage();
	    rebuildCache();
	    return this;
	}
	
	public ContactHelper deleteContact(int index) {
		manager.navigateTo().mainPage();
	    selectContactByIndex(index);
		submitContactDeletion();
		returnToHomePage();
	    rebuildCache();
	    return this;

	}

	//---------------------------------------------------------------------------
	
	public ContactHelper initContactCreation() {
	    click(By.linkText("add new"));
	    return this;
	  }

	public ContactHelper fillContactForm(ContactData contact) {
		type(By.name("firstname"), contact.getFirstname());
		type(By.name("lastname"), contact.getLastname());
		type(By.name("address"), contact.getAddress1());
		type(By.name("home"), contact.getHomephone());
		type(By.name("mobile"), contact.getMobilephone());
		type(By.name("work"), contact.getWorkphone());
		type(By.name("email"), contact.getEmail1());
		type(By.name("email2"), contact.getEmail2());
	    selectByText(By.name("bday"), contact.getBday());
	    selectByText(By.name("bmonth"), contact.getBmonth());
	    //selectByText(By.name("group"), "group 1");
	    type(By.name("byear"), contact.getByear());
	    //if (formType == CREATION) {
	   // } else {
	    	//if(driver.findElement(By.name("new_group")).Size() != 0) {
	    		//throw new Error ("Group selector exist in contact modification form");
	    	//}
	   // }
	    type(By.name("address2"), contact.getAddress2());
        type(By.name("phone2"), contact.getHomephone2());
	    return this;
	  }

	
	public ContactHelper submitContactCreation() {
	    click(By.name("submit"));
	    return this;
	  }

	public ContactHelper returnToHomePage() {
	    click(By.linkText("home"));
	    return this;
	  }

	private void selectContactByIndex(int index) {
		click(By.xpath("//tr[" + (index+2) + "]/td[7]/a/img"));
	}

	public ContactHelper initContactModification(int index) {
		selectContactByIndex(index);
	    return this;
	}

	public ContactHelper submitContactModification() {
		click(By.name("update"));
		cachedContacts = null;
	    return this;
	}

	private void submitContactDeletion() {
		click(By.xpath("//div[@id='content']/form[2]/input[2]"));
		cachedContacts = null;
	}
}
